from _md5 import md5
from dataclasses import dataclass
from datetime import datetime
from hashlib import sha256
from typing import List

from sqlalchemy import Boolean

import models
from exts import db

@dataclass
class Power(db.Model):
    __tablename__ = 'power'
    id:int = db.Column(db.Integer, primary_key=True, comment='权限编号')
    name:str = db.Column(db.String(255), comment='权限名称')
    type:int = db.Column(db.Integer, comment='权限类型')
    code:str = db.Column(db.String(255), comment='权限标识')
    url:str = db.Column(db.String(255), comment='权限路径')
    open_type:str = db.Column(db.String(255), comment='打开方式')
    parent_id:int = db.Column(db.Integer, comment='父类编号')
    icon:str = db.Column(db.String(255), comment='图标')
    sort:int = db.Column(db.Integer, comment='排序')
    create_time:datetime = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    update_time:datetime = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now,
                                comment='更新时间')
    enable:bool = db.Column(Boolean, comment='是否开启',default=True)

    def to_dict(self):
        power_dict={
            'id':self.id,
            'name':self.name,
            'type':self.type,
            'code':self.code,
            'url':self.url,
            'open_type':self.open_type,
            'parent_id':self.parent_id,
            'icon':self.icon,
            'sort':self.sort,
            'enable':self.enable,
        }
        return power_dict
    @classmethod
    def to_model(cls, **kwargs):
        model = Power()  # 实例化一个device对象
        columns = [c.name for c in cls.__table__.columns]  # 获取Devices模型定义的所有列属性的名字
        for k, v in kwargs.items():  # 遍历传入kwargs的键值
            if k in columns:  # 如果键包含在列名中，则为该device对象赋加对应的属性值
                setattr(model, k, v)
        return model


